home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CTOOLS10.ARJ / CSTUB.C < prev    next >
C/C++ Source or Header  |  1991-12-31  |  6KB  |  180 lines

  1. /****************************************************************************
  2. *
  3. *                    Copyright (C) 1991 Kendall Bennett.
  4. *                            All rights reserved.
  5. *
  6. * Filename:        $RCSfile: cstub.c $
  7. * Version:        $Revision: 1.4 $
  8. *
  9. * Language:        ANSI C
  10. * Environment:    any
  11. *
  12. * Description:    A C program stub containing common interface code to getopt
  13. *                and other routines.
  14. *
  15. * $Id: cstub.c 1.4 91/12/31 19:39:36 kjb Exp $
  16. *
  17. * Revision History:
  18. * -----------------
  19. *
  20. * $Log:    cstub.c $
  21. * Revision 1.4  91/12/31  19:39:36  kjb
  22. * Modified include file directories.
  23. * Revision 1.3  91/09/25  14:46:39  kjb
  24. * Added code for UNIX.
  25. * Revision 1.2  91/09/24  19:47:46  kjb
  26. * Added code to interface to the getargs() routine.
  27. * Revision 1.1  91/08/16  11:19:56  ROOT_DOS
  28. * Initial revision
  29. ****************************************************************************/
  30.  
  31. char    *rcsid = "$Id: cstub.c 1.4 91/12/31 19:39:36 kjb Exp $";
  32.  
  33. #include <stdio.h>
  34. #include <malloc.h>
  35. #include <process.h>
  36. #include <string.h>
  37. #include <ctype.h>
  38.  
  39. #ifdef    __MSDOS__
  40. #include <dir.h>
  41. #endif
  42.  
  43. #include "debug.h"
  44. #include "getopt.h"                    /* Option parsing routines            */
  45.  
  46. /*------------------------- Global variables ------------------------------*/
  47.  
  48. char    *version = "1.1b";            /* Version string (eg: 5.20b)        */
  49. char    nameofus[MAXFILE];            /* Name of program (no path)        */
  50. char    pathtous[MAXDIR];            /* Pathname to our program.            */
  51. char    *progname;                    /* Descriptive name of program        */
  52.  
  53. option    optarr[] =
  54.    {{'d',INTEGER,NULL,"An integer number"},
  55.     {'x',HEX,NULL,"A hexidecimal number"},
  56.     {'z',SWITCH,NULL,"An option switch"}};
  57.  
  58. /*-------------------------- Implementation -------------------------------*/
  59.  
  60. void init(char *argv0,char *prognam)
  61. /****************************************************************************
  62. *                                                                              *
  63. * Function:        init                                                        *
  64. * Parameters:    char    *argv0        - The argv[0] array entry.                *
  65. *                char    *prognam    - Descriptive name of program.            *
  66. *                                                                           *
  67. * Description:    Init takes the pathname to our program as a parameter        *
  68. *                (found in argv[0]) and use this to set up three global        *
  69. *                variables:                                                  *
  70. *                                                                           *
  71. *                pathtous    - Contains the pathname to our program          *
  72. *                nameofus    - Contains the name of the program (without the *
  73. *                              .EXE extension)                               *
  74. *                                                                            *
  75. *                We also set up the global variable progname to point to     *
  76. *                the static string passed to init for all to use.            *
  77. *                                                                           *
  78. ****************************************************************************/
  79. {
  80.     char    *p,i;
  81.  
  82.     /* Obtain the path to our program from pathname - note that we only
  83.      * do this for MS DOS machines. Under UNIX this is not available
  84.      * since argv[0] holds the name of the program without the path
  85.      * attached. We set pathtous to an empty string under UNIX, and
  86.      * nameofus to the value of argv[0].
  87.      */
  88.  
  89. MS(    p = strrchr(argv0,'\\') + 1;
  90.     i = *p;
  91.     *p = '\0';
  92.     strcpy(pathtous,argv0);
  93.     *p = i;
  94.  
  95.     /* Obtain the name of our program from pathname */
  96.  
  97.     i = 0;
  98.     while (*p != '.')
  99.         nameofus[i++] = *p++;
  100.     nameofus[i] = '\0';)
  101.  
  102. UX(    strcpy(nameofus,argv0);
  103.     pathtous[0] = '\0';)
  104.  
  105.     progname = prognam;
  106. }
  107.  
  108. void banner(void)
  109. /****************************************************************************
  110. *
  111. * Function:     banner
  112. *
  113. * Description:  Prints the program's banner to the standard output
  114. *                Under Borland C++, we insert the compilation date into
  115. *                the banner using the __DATE__ macro. This does not
  116. *                seem to be available under some UNIX systems, so for UNIX
  117. *                we do not insert the date into the banner.
  118. *
  119. ****************************************************************************/
  120. {
  121.     MS(printf("%s  Version %s - %s  Copyright (C) 1991 Kendall Bennett\n\n"
  122.         ,progname,version,__DATE__);)
  123.     UX(printf("%s  Version %s  Copyright (C) 1991 Kendall Bennett\n\n"
  124.         ,progname,version);)
  125. }
  126.  
  127. void help(void)
  128. /****************************************************************************
  129. *
  130. * Function:     help
  131. *
  132. * Description:  Help provides usage information about our program if the
  133. *               options do make any sense or the help switch was specified
  134. *                on the command line.
  135. *
  136. ****************************************************************************/
  137. {
  138.     banner();
  139.     printf("Usage: %s [options]\n\n",nameofus);
  140.     printf("Options are:\n");
  141.     print_desc(NUM_OPT,optarr);
  142.     exit(1);
  143. }
  144.  
  145. int do_params(char *param,int num)
  146. /****************************************************************************
  147. *
  148. * Function:        do_params
  149. * Parameters:    param    - String representing parameter
  150. *                num        - Parameter number
  151. * Returns:        ALLDONE on success, or INVALID on error.
  152. *
  153. * Description:    Handles the parameters on the command line, interspersed
  154. *                between command line options.
  155. *
  156. ****************************************************************************/
  157. {
  158.     switch (num) {
  159.         }
  160.     return ALLDONE;
  161. }
  162.  
  163. void main(int argc,char **argv)
  164. {
  165.     init(argv[0],"CStub");            /* Initialise a few globals            */
  166.  
  167.     switch (getargs(argc,argv,NUM_OPT,optarr,do_params)) {
  168.         case INVALID:
  169.             printf("Invalid option\a\n");
  170.             exit(1);
  171.             break;
  172.         case HELP:
  173.             help();
  174.         }
  175. }
  176.